package com.boot.main.service.impl;

import com.boot.main.utils.IsNotEmptyUtil;
import com.boot.main.entity.CcomDict;
import com.boot.main.mapper.CcomDictMapper;
import com.boot.main.response.ResponseData;
import com.boot.main.response.ResponseDataUtil;
import com.boot.main.service.IDictService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class DictService implements IDictService {
    @Resource
    private CcomDictMapper dictMapper;

    @Override
    public ResponseData getDicts(Integer page, Integer limit, String name){
        List<CcomDict> items = dictMapper.getDicts((page-1)*limit, limit, name);
        Integer count = dictMapper.getTotalCount();
        Map map = new HashMap();
        map.put("datas", items);
        map.put("count", count);
        if(!items.isEmpty()){
            for (CcomDict item:items){
            }
            return ResponseDataUtil.success( "查询成功", map);
        }
        return ResponseDataUtil.success("暂无字典信息！", map);
    }

    @Override
    public ResponseData saveDict(CcomDict item){
        int item_num = dictMapper.saveDict(item);
        long item_id = item.getId();
        if(!IsNotEmptyUtil.isEmpty(item_num)) {
            return ResponseDataUtil.success( "新增字典成功！", null);
        } else {
            return ResponseDataUtil.error("新增字典失败！", null);
        }
    }

    @Override
    public ResponseData updateDict(CcomDict item){
        int item_num = dictMapper.updateDict(item);
        if(!IsNotEmptyUtil.isEmpty(item_num)) {
            return ResponseDataUtil.success( "修改字典成功！", null);
        } else {
            return ResponseDataUtil.error( "修改字典失败！", null);
        }
    }

    @Override
    public ResponseData deleteDict(Long id){
        int item_num = dictMapper.deleteDict(id);
        if(!IsNotEmptyUtil.isEmpty(item_num)) {
            return ResponseDataUtil.success("删除字典成功！", null);
        } else {
            return ResponseDataUtil.error( "删除字典失败！", null);
        }
    }
}
